package com.lyp.qczp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.lyp.qczp.dao.Organizadao;
import com.lyp.qczp.entity.Organiza;
@Service
public class OrganizaServiceImp implements OrganizaService{
	@Autowired
	private Organizadao organizadao;
	
	@Override
	public int save(Organiza organiza) {
		Organiza save = organizadao.save(organiza);
		if (save!=null) {
			return 1;
		}
		return 0;
	}

	@Override
	public int deleteById(Integer reId) {
		organizadao.deleteById(reId);
		return 1;
	}
	@Override
	public List<Organiza> selectfindAll() {
		return organizadao.findAll();
	}
	
	@Override
	public Map<String, Object> findAll(PageRequest pageable, Organiza organiza) {
		Page<Organiza> findAll = organizadao.findAll(selectquanpage(organiza),pageable);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", findAll.getContent());
		map.put("total", findAll.getTotalElements());
		return map;
	}
	//条件查询Specification
			private Specification<Organiza> selectquanpage(Organiza organiza){
				return new Specification<Organiza>() {

					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					@Override
					public Predicate toPredicate(Root<Organiza> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
						// TODO Auto-generated method stub
						Predicate conjunction = cb.conjunction();
						List<Expression<Boolean>> expressions = conjunction.getExpressions();
						if (organiza.getOParent()!=null) {
							expressions.add(cb.equal(root.<Integer>get("oParent"), organiza.getOParent()));
						}
						
						
						return conjunction;
					}
				};
			}

			@Override
			public List<Organiza> selectbyfu(Integer oParent) {
				// TODO Auto-generated method stub
				if (oParent==null) {
					oParent=0;
				}
				return organizadao.selectbyfu(oParent);
			}
}
